package winOrDie.Net.P2P;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import rice.p2p.commonapi.NodeHandle;
import rice.p2p.scribe.ScribeContent;

public class WoD_ScribeContent implements ScribeContent {

	public static class Message_Types
	{
		public static final int NEW_NODE = 0;
		public static final int NODE_DOWN = 1;
		public static final int NEW_ROOT = 2;
		public static final int APPLICATION = 3;
	}
	
	public static class Message_Headers
	{
		public static final String NEW_NODE = "NEW_NODE";
		public static final String NODE_DOWN = "NODE_DOWN";
		public static final String NEW_ROOT = "NEW_ROOT";
		public static final String APPLICATION = "APPLICATION";
	}
	
	
	
	
	/**
	 * The source of this content.
	 */
	NodeHandle from;
	
	/**
	 * The destination of this content. Only important for anycast messages.
	 */
	String destination_Node_ID;

	/**
	 * The sequence number of the content.
	 */
	int seq;

	/**
	 * The message Type 
	 */
	int messageType = -1;
	
	/**
	 * The message content
	 */
	byte[] message;
	
	

	public NodeHandle getFrom() {
		return from;
	}


	public String getDestination_Node_ID() {
		return destination_Node_ID;
	}


	public void setDestination_Node_ID(String destinationNodeID) {
		destination_Node_ID = destinationNodeID;
	}


	public byte[] getMessage() {
		return message;
	}

	
	

	public void setMessage(String message) throws Exception
	{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
		
		dataOutputStream.writeUTF(message);
		
		setMessage(byteArrayOutputStream.toByteArray());
	}
	
	public void setMessage(byte[] message) {
		this.message = message;
	}


	public int getMessageType() {
		return messageType;
	}

	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	/**
	 * Simple constructor.  Typically, you would also like some
	 * interesting payload for your application.
	 * 
	 * @param from Who sent the message.
	 * @param seq the sequence number of this content.
	 */
	public WoD_ScribeContent(NodeHandle from, int seq) {
		this.from = from;
		this.seq = seq;
		
		//	    System.out.println(this+".ctor");
	}

	
	
	/**
	 * Ye ol' toString() 
	 */
	public String toString() {
		
		return "MyScribeContent #"+seq+" from "+from;
	} 
}
